﻿using ServerBase.Config;
using SvGame.Objects;
using System.Xml.Linq;

namespace SvGame.Logic;

public static partial class GameLogic
{
    public static bool NewEquip(ConfigEquip conf, out Equip obj)
    {
        obj = new()
        {
            Uid = IdMgr.CreateObjectId(),
            ConfigId = conf.Id,
            Cfg = conf,
        };
        return true;
    }
    /// <summary>
    /// 玩家添加装备
    /// </summary>
    /// <param name="player"></param>
    /// <param name="equip"></param>
    /// <returns></returns>
    public static bool AddEquip(this Player player, int configId, long count = 1)
    {
        if (configId == 0)
        {
            return false;
        }
        if (count <= 0)
        {
            loger.Warn($"Player AddEquip 数目错误 configId{configId} Count{count}");
            return false;
        }
        if (!ConfEquip.TryGetValue(configId, out var conf))
        {
            loger.Warn($"Player AddEquip 装备配置错误 ConfigId{configId}");
            return false;
        }
        for (int i = 0; i < count; i++)
        {
            if (NewEquip(conf, out var obj))
            {
                player.AddEquip(obj);
                obj.Env = player;
            }
        }
        return true;
    }
    /// <summary>
    /// 玩家添加装备
    /// </summary>
    /// <param name="player"></param>
    /// <param name="equip"></param>
    /// <returns></returns>
    public static bool AddEquip(this Player player, Equip obj)
    {
        obj.Env = player;
        player.DictEquip[obj.Uid] = obj;
        return true;
    }
    /// <summary>
    /// 玩家从房间拾取道具
    /// </summary>
    /// <param name="player"></param>
    /// <param name="item"></param>
    public static void EquipGetFromRoom(this Player player, Equip obj)
    {
        player.Room.DictEquip.Remove(obj.Uid);
        player.AddEquip(obj);
    }
    /// <summary>
    /// 玩家丢下道具到房间
    /// </summary>
    /// <param name="player"></param>
    /// <param name="item"></param>
    public static void EquipDropToRoom(this Player player, Equip obj)
    {
        player.Room.DictEquip[obj.Uid] = obj;
        player.DictEquip.Remove(obj.Uid);
    }

    // EquipFinish 装备完毕处理
    public static void EquipFinish(this Player player)
    {
        player.ActionSet();
        player.PropsEquipSet();
        player.HpSet();
        player.NtfPlayerInfo();
        player.NtfPlayerPerform();
    }
    /// <summary>
    /// 装备穿上
    /// </summary>
    /// <param name="me"></param>
    /// <param name="equip"></param>
    /// <param name="bmsg"></param>
    public static void EquipWear(this Player me, Equip equip, bool bmsg)
    {
        var type = (EEquipType)equip.Cfg.EquipType;
        me.DictWear[type] = equip.Uid;
        if (bmsg)
        {
            var DescWear = equip.Cfg.DescWear;
            if (DescWear.IsNullOrEmpty(true))
            {
                switch (type)
                {
                    case EEquipType.武器:
                        DescWear = "$N抄起一$u$n，握在手中当武器。";
                        break;
                    case EEquipType.副手:
                        DescWear = "$N抓起一$u$n，握在手中。";
                        break;
                    case EEquipType.衣服:
                    case EEquipType.披风:
                    case EEquipType.鞋子:
                        DescWear = "$N穿上一$u$n。";
                        break;
                    case EEquipType.头盔:
                    case EEquipType.项链:
                    case EEquipType.护腰:
                    case EEquipType.戒指:
                    case EEquipType.手套:
                        DescWear = "$N戴上一$u$n。";
                        break;
                    case EEquipType.护腕:
                        DescWear = "$N佩上一$u$n。";
                        break;
                    default:
                        DescWear = "$N装备上$n。";
                        break;
                }
            }
            DescWear = DescWear.Replace("$u", equip.Cfg.Unit);
            DescWear = DescWear.Replace("$n", equip.Cfg.Name);
            MessageVision(DescWear, me);
        }
    }
    /// <summary>
    /// 装备脱下
    /// </summary>
    /// <param name="me"></param>
    /// <param name="type"></param>
    /// <param name="bmsg"></param>
    public static void EquipUnwear(this Player me, EEquipType type, bool bmsg)
    {
        if (bmsg)
        {
            if (me.DictWear.TryGetValue(type, out var uid))
            {
                if (me.DictEquip.TryGetValue(uid, out var equip))
                {
                    var DescWear = equip.Cfg.DescRemove;
                    if (DescWear.IsNullOrEmpty(true))
                    {
                        switch (type)
                        {
                            case EEquipType.武器:
                            case EEquipType.副手:
                                DescWear = "$N放下手中的$n。";
                                break;
                            case EEquipType.衣服:
                            case EEquipType.披风:
                            case EEquipType.鞋子:
                                DescWear = "$N将$n脱了下来。";
                                break;
                            default:
                                DescWear = "$N卸下$n的装备。";
                                break;
                        }
                    }
                    DescWear = DescWear.Replace("$u", equip.Cfg.Unit);
                    DescWear = DescWear.Replace("$n", equip.Cfg.Name);
                    MessageVision(DescWear, me);
                }
            }
        }
        me.DictWear.Remove(type);
    }

}
